import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;


/**
 * Thanks to http://computercranium.com/programming/java/java-jlabel-hyperlink for the following class.
 * This class creates hyperlinks that look and act like hyperlinks on a web page.
 */
public class JHyperlink extends JLabel implements MouseListener {
	private URI uri = null;
	
	/**
	 * Constructor
	 * 
	 * @param text
	 * @param uri
	 */
	public JHyperlink (String text, String uri){
		this.setText( "<html><u>" + text + "</u></html>" );
	
		try {

		this.uri = new URI( uri );
	
		} catch (URISyntaxException e) {

		e.printStackTrace();

		}

		initialize();
	}
	
	/**
	 * This method returns the URI
	 * @return
	 */
	public URI getURI(){
		return uri;
	}
	
	/**
	 * This method initializes the
	 * link.
	 */

	private void initialize() {

		this.setForeground( Color.BLUE );
		this.setOpaque( false );
		this.setToolTipText( getURI().toString() );
		this.addMouseListener( this );

	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if ( e.getClickCount() > 0 ) {
			if ( Desktop.isDesktopSupported() ) {
				Desktop desktop = Desktop.getDesktop();
				try {
					desktop.browse( getURI() );
				} catch ( IOException ex ) {
					ex.printStackTrace();
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		this.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		this.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		this.setForeground( Color.RED );
		this.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) );
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		this.setForeground( Color.BLUE );
		this.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
	}

}
